package com.willcoo.autoupdatelib.utils;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Environment;
import android.os.IBinder;
import android.text.TextUtils;
import android.widget.RemoteViews;
import android.widget.Toast;

import com.willcoo.autoupdatelib.AutoUpdateUtil;
import com.willcoo.autoupdatelib.R;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.URL;

import static com.willcoo.autoupdatelib.AutoUpdateUtil.installApkFile;

public class DownloadService extends Service {
    private static final int NOTIFY_ID = 0;

    private Context mContext = this;
    private NotificationManager mNotificationManager;
    private Notification mNotification;
    private Notification.Builder builder;
    private DownFileAsyncTask downFileAsyncTask;
    private String downUrl;
    private String appName;
    private int showType;
    private int lastPosition;
    private boolean autoCancel;

    @Override
    public void onCreate() {
        super.onCreate();
        mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        downUrl = intent.getStringExtra("downUrl");
        String fileName=intent.getStringExtra("fileName");
        appName = intent.getStringExtra("appName");
        showType = intent.getIntExtra("type", 0);
        if (!TextUtils.isEmpty(downUrl)) {
            if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
                //后台下载或者前台展示后台下载都是调用这个
                if (showType == AutoUpdateUtil.Builder.TYPE_NITIFICATION || showType == AutoUpdateUtil.Builder.TYPE_DIALOG_WITH_BACK_DOWN) {
                    builder = new Notification.Builder(mContext);
                    if (intent.getIntExtra("icRes", 0) != 0) {
                        builder.setSmallIcon(intent.getIntExtra("icRes", 0));
                    } else {
                        builder.setSmallIcon(R.mipmap.ic_launcher1); //设置图标
                    }
                    RemoteViews contentView = new RemoteViews(mContext.getPackageName(), R.layout.layout_notification);
                    if (TextUtils.isEmpty(appName))
                        contentView.setTextViewText(R.id.fileName, "正在下载...");
                    else
                        contentView.setTextViewText(R.id.fileName, appName + "正在下载...");
                    builder.setContent(contentView);
                    mNotification = builder.build();
                    mNotificationManager.notify(NOTIFY_ID, mNotification);
                } else {
                    mNotificationManager = null;
                }
                downFileAsyncTask = new DownFileAsyncTask();
                downFileAsyncTask.execute(fileName);
            } else {
                Toast.makeText(mContext, "没有挂载的SD卡", Toast.LENGTH_SHORT).show();
            }
        } else {
            throw new RuntimeException("下载地址不能为空");
        }

        return START_REDELIVER_INTENT;
    }

    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (downFileAsyncTask != null) {
            downFileAsyncTask.cancel(true);
        }
    }

    //异步任务下载文件
    class DownFileAsyncTask extends AsyncTask<String, Integer, File> {

        @Override
        protected File doInBackground(String... params) {
            try {
                FileOutputStream fos = null;
                BufferedInputStream bis = null;
                HttpURLConnection httpUrl = null;
                URL url = null;
                byte[] buf = new byte[2048];
                int size = 0;
                //建立链接
                url = new URL(downUrl);
                httpUrl = (HttpURLConnection) url.openConnection();
                //设置网络连接超时时间5S
                httpUrl.setConnectTimeout(10 * 1000);
                //连接指定的资源
                httpUrl.connect();
                //获取网络输入流
                bis = new BufferedInputStream(httpUrl.getInputStream());
                //建立文件
                File file = new File(params[0]);
                if (!file.exists()) {
                    if (!file.getParentFile().exists()) {
                        file.getParentFile().mkdirs();
                    }
                    file.createNewFile();
                }
                fos = new FileOutputStream(params[0]);

                final long total = httpUrl.getContentLength();
                long sum = 0;

                //保存文件
                while ((size = bis.read(buf)) != -1) {
                    sum += size;
                    fos.write(buf, 0, size);
                    publishProgress((int) (sum * 100 / total));
                }
                fos.close();
                bis.close();
                httpUrl.disconnect();
                return file;
            } catch (IOException e) {
                //发送特定action的广播
                Intent intent = new Intent();
                intent.setAction("android.intent.action.MY_RECEIVER");
                intent.putExtra("type", "err");
                intent.putExtra("err", e.toString());
                sendBroadcast(intent);
            } finally {
            }
            return null;
        }

        @Override
        protected void onProgressUpdate(Integer... values) {
            super.onProgressUpdate(values);
            int rate = values[0];
            if ((rate % 1 == 0 || rate == 100) && rate != lastPosition) {
                lastPosition = rate;
                if (showType == AutoUpdateUtil.Builder.TYPE_NITIFICATION || showType
                        == AutoUpdateUtil.Builder.TYPE_DIALOG_WITH_BACK_DOWN) {
                    if (rate < 100) {
                        //更新进度
                        RemoteViews contentView = mNotification.contentView;
                        contentView.setTextViewText(R.id.rate, rate + "%");
                        contentView.setProgressBar(R.id.progress, 100, rate, false);
                        // 最后别忘了通知一下,否则不会更新
                        mNotificationManager.notify(NOTIFY_ID, mNotification);
                    } else {
                        //下载完毕后变换通知形式
                        mNotificationManager.cancel(NOTIFY_ID);
                        mNotification.contentView = null;
                    }

                    autoCancel = false;
                    if (rate >= 100) {
                        mNotification.flags = Notification.FLAG_AUTO_CANCEL;
                        autoCancel = true;
                    }
                }
                //发送特定action的广播
                Intent intent = new Intent();
                intent.setAction("android.intent.action.MY_RECEIVER");
                intent.putExtra("type", "doing");
                intent.putExtra("progress", rate);
                sendBroadcast(intent);
            }
        }

        @Override
        protected void onPostExecute(File file) {
            super.onPostExecute(file);
            if (file != null) {
                installApkFile(mContext, file);
            } else {
                if (showType != AutoUpdateUtil.Builder.TYPE_DIALOG) {
                    mNotificationManager.cancel(NOTIFY_ID);
                    mNotification.contentView = null;
                }
            }
            stopSelf(); //停止服务
        }

        @Override
        protected void onCancelled() {
            super.onCancelled();
            if (!autoCancel)
                Toast.makeText(mContext, "已取消下载", Toast.LENGTH_LONG).show();
        }
    }
}